﻿
/*
* anchorMcs : Array
* anchorMcs = [ {mc:MovieClip,anchor:Number,x:Number,y:Number,width:Number,height:Number},			// anchor="top",anchor="left",anchor="right",anchor="bottom",anchor="fill",anchor="all"
* 				{mc:MovieClip,anchor:Number,x:Number,y:Number,width:Number,height:Number} 
* 			  ]
* 
* 
* */

dynamic class AutoLayOut{
	public static var AM_top=1;
	public static var AM_left=2;
	public static var AM_right=4;
	public static var AM_bottom=8;
	
	private var anchorMcs:Array;
	private var swfSize:Object;  //{width:Number, height:Number,x:Number,y:Number}
	private var dockStage:Boolean;
	private var parent:Object;
	private var reanchor:Boolean;
	
	public static var say:Function;
	
	/////////////////////////////////////////
	
	public function get Reanchor():Boolean{
		return this.reanchor;
	}
	public function set Reanchor(value:Boolean){
		this.reanchor=value;
	}
	
	////////////////////////////////////////////
	
	
	
	/**
	* an auto anchor mc class
	* @param	width 			old width of frame or mc
	* @param	height			old height of frame or mc
	* @param	dockparent	  	parent mc,if not an mc,keep it null
	*/
	public function AutoLayOut(width:Number,height:Number,dockparent:MovieClip){
		if(dockparent==undefined || dockparent==null){
			dockStage=true;
		}else{
			dockStage=false;
		}
		parent=dockparent;
		
		initVars(width,height);
	}
	
	private function initVars(width:Number,height:Number){
		if(width!=undefined && height!=undefined){
			if(dockStage){
				this.swfSize={width:width,height:height,x:0,y:0};
			}else{
				this.swfSize={width:width,height:height,x:parent._x,y:parent._y};
			}
		}else{
			if(dockStage){
				parent=Stage;
				this.swfSize={width:Stage.width,height:Stage.height,x:0,y:0};
			}else{
				var p:MovieClip=MovieClip(this.parent);
				this.swfSize={width:p._width,height:p._height,x:parent._x,y:parent._y};
			}
		}
		reanchor=false;
			if(dockStage){
				parent=Stage;
				Stage.scaleMode="noScale";
			}
		this.anchorMcs=new Array();
	}
	/**
	* add an mc to anchor
	* @param	mc MovieClip
	* @param	anchor	anchor way,use AutoLayOut's static vars ,like "AM_top","AM_left","AM_right","AM_bottom","AM_fill","AM_all"
	*/
	
	public function AddMc(mc:MovieClip,anchor:Number,fill:Boolean){
		if(mc==undefined || anchor==undefined || anchor>0xf) {
			this.Say("**error** can't add empty MovieClip or add mc not assign the way to anchor");
			return ;
		}
		if(fill==true){
			mc._x=this.swfSize.x;
			mc._y=this.swfSize.y;
			mc._width=this.swfSize.width;
			mc._height=this.swfSize.height;
		}
		if(anchor>0){
			this.anchorMcs.push({mc:mc,anchor:anchor,x:mc._x,y:mc._y,width:mc._width,height:mc._height});
			this.AnchorAll();
		}else{
			this.Say("**error** the anchor is worng");
		}
	}
	public function AnchorAll(){
		this.anchorAllMcs();
	}
	private function anchorAllMcs(){
		
		if(this.dockStage){
			this.anchorMcInStage();
		}else{
			this.anchorMcInMc();
		}
		
	}
	private function anchorMcInMc(){
		var wl=this.parent._width-this.swfSize.width;
		var hl=this.parent._height-this.swfSize.height;
		
		var bw=this.parent._width/this.swfSize.width;
		var bh=this.parent._height/this.swfSize.height;
		
		var xl=this.parent._x-this.swfSize.x;
		var yl=this.parent._y-this.swfSize.y;
		
		for(var i in this.anchorMcs){
			if(this.anchorMcs==undefined) {
				return;
			}
			var obj:Object=this.anchorMcs[i];
			var mc:MovieClip=MovieClip(obj.mc);
			mc._x=obj.x;
			mc._y=obj.y;
			mc._width=obj.width;
			mc._height=obj.height;
			switch(obj.anchor){
				case 1:
				mc._y=obj.y+yl;
				break;
				
				case 2:
				mc._x=obj.x+xl;
				break;
				
				case 3:
				mc._x=obj.x+xl;
				mc._y=obj.y+yl;
				break;
				
				case 4:
				mc._x=obj.x+wl;
				trace(mc);
				break;
				
				case 5:
				mc._x=obj.x+wl;
				break;
				
				case 6:
				mc._x=obj.x+xl;
				mc._y=obj.y+yl;
				mc._width=Math.max(0,obj.width+wl);
				break;
				
				case 7:
				mc._x=obj.x+xl;
				mc._y=obj.y+yl;
				mc._width=Math.max(0,obj.width+wl);
				break;
				
				case 8:
				mc._y=obj.y+hl+yl;
				break;
				
				case 9:
				mc._y=obj.y+yl;
				mc._height=Math.max(0,obj.height+hl);
				break;
				
				case 10:
				mc._y=obj.y+yl+hl;
				mc._x=obj.x+xl;
				break;
				
				case 11:
				mc._y=obj.y+yl;
				mc._x=obj.x+xl;
				mc._height=Math.max(0,obj.height+hl);
				break;
				
				case 12:
				mc._y=obj.y+yl+hl;
				mc._x=obj.x+xl+wl;
				break;
				
				case 13:
				mc._y=obj.y+yl;
				mc._x=obj.x+xl+wl;
				mc._height=Math.max(0,obj.height+hl);
				break;
				
				case 14:
				mc._y=obj.y+yl+hl;
				mc._x=obj.x+xl;
				mc._width=Math.max(0,obj.width+wl);
				break;
				
				case 15:
				mc._y=obj.y+yl;
				mc._x=obj.x+xl;
				mc._height=Math.max(0,obj.height+hl);
				mc._width=Math.max(0,obj.width+wl);
				break;
			}
			mc._x=int(mc._x);
			mc._y=int(mc._y);
		}
	}
	private function anchorMcInStage(){
		var wl=this.parent.width-this.swfSize.width;
		var hl=this.parent.height-this.swfSize.height;
		
		var bw=this.parent.width/this.swfSize.width;
		var bh=this.parent.height/this.swfSize.height;
		
		var xl=this.parent._x-this.swfSize.x;
		var yl=this.parent._y-this.swfSize.y;
		
		for(var i in this.anchorMcs){
			if(this.anchorMcs==undefined) {
				return;
			}
			var obj:Object=this.anchorMcs[i];
			var mc:MovieClip=MovieClip(obj.mc);
			mc._x=obj.x;
			mc._y=obj.y;
			mc._width=obj.width;
			mc._height=obj.height;
			switch(obj.anchor){
				case 1:
				if(!this.reanchor){
					mc._y=obj.y-(hl/2);
				}else{
					mc._y=this.swfSize.height/2-(this.swfSize.height/2-obj.y)*bh;
				}
				break;
				
				case 2:
				if(!this.reanchor){
					mc._x=obj.x-(wl/2);
				}else{
					mc._x=this.swfSize.width/2-(this.swfSize.width/2-obj.x)*bw;
						 
				}
				break;
				
				case 3:
				if(!this.reanchor){
					mc._x=obj.x-(wl/2);
					mc._y=obj.y-(hl/2);
				}
				else{
					trace(bh)
					mc._y=this.swfSize.height/2-(this.swfSize.height/2-obj.y)*bh;
					mc._x=this.swfSize.width/2-(this.swfSize.width/2-obj.x)*bw;
				}
				break;
				
				case 4:	
				if(!this.reanchor){
					mc._x=obj.x+(wl/2);
				}else{
					mc._x=this.swfSize.width/2-(this.swfSize.width/2-obj.x)*bw;
				}
				break;
				
				case 5:
				if(!this.reanchor){
					mc._y=obj.y-(hl/2);
					mc._x=obj.x+(wl/2);
				}else{
					mc._y=this.swfSize.height/2-(this.swfSize.height/2-obj.y)*bh;
					mc._x=this.swfSize.width/2-(this.swfSize.width/2-obj.x)*bw;
				}
				break;
				
				case 6:
				mc._x=obj.x-(wl/2);
				mc._width=Math.max(0,obj.width+wl);
				break;
				
				case 7:
				if(!this.reanchor){
					mc._y=obj.y-(hl/2);
					mc._x=obj.x-(wl/2);
					mc._width=Math.max(0,obj.width+wl);
				}else{
					mc._y=this.swfSize.height/2-(this.swfSize.height/2-obj.y)*bh;
					mc._x=obj.x-(wl/2);
					mc._width=Math.max(0,obj.width+wl);
				}
				break;
				
				case 8:
				if(!this.reanchor){
					mc._y=obj.y+(hl/2);
				}else{
					mc._y=this.swfSize.height/2-(this.swfSize.height/2-obj.y)*bh;
				}
				break;
				
				case 9:
				mc._y=obj.y-(hl/2);
				mc._height=Math.max(0,obj.height+hl);
				break;
				
				case 10:
				if(!this.reanchor){
					mc._x=obj.x-(wl/2);
					mc._y=obj.y+(hl/2);
				}else{
					mc._y=this.swfSize.height/2-(this.swfSize.height/2-obj.y)*bh;
					mc._x=this.swfSize.width/2-(this.swfSize.width/2-obj.x)*bw;
				}
				break;
				
				case 11:
				if(!this.reanchor){
					mc._x=obj.x-(wl/2);
					mc._y=obj.y-(hl/2);
					mc._height=Math.max(0,obj.height+hl);
				}else{
					mc._x=this.swfSize.width/2-(this.swfSize.width/2-obj.x)*bw;
					mc._y=obj.y-(hl/2);
					mc._height=Math.max(0,obj.height+hl);
				}
				break;
				
				case 12:
				if(!this.reanchor){
					mc._x=obj.x+(wl/2);
					mc._y=obj.y+(hl/2);
				}else{
					mc._y=this.swfSize.height/2-(this.swfSize.height/2-obj.y)*bh;
					mc._x=this.swfSize.width/2-(this.swfSize.width/2-obj.x)*bw;
				}
				break;
				
				case 13:
				if(!this.reanchor){
					mc._x=obj.x+(wl/2);
					mc._y=obj.y-(hl/2);
					mc._height=Math.max(0,obj.height+hl);
				}else{
					mc._x=this.swfSize.width/2-(this.swfSize.width/2-obj.x)*bw;
					mc._y=obj.y-(hl/2);
					mc._height=Math.max(0,obj.height+hl);
				}
				break;
				
				case 14:
				if(!this.reanchor){
					mc._y=obj.y+(hl/2);
					mc._x=obj.x-(wl/2);
					mc._width=Math.max(0,obj.width+wl);
				}else{
					mc._y=this.swfSize.height/2-(this.swfSize.height/2-obj.y)*bh;
					mc._x=obj.x-(wl/2);
					mc._width=Math.max(0,obj.width+wl);
				}
				break;
				//
				case 15:
				mc._x=obj.x-(wl/2);
				mc._y=obj.y-(hl/2);
				mc._height=Math.max(0,obj.height+hl);
				mc._width=Math.max(0,obj.width+wl);
				break;
			}
			mc._x=int(mc._x);
			mc._y=int(mc._y);
		}
	}
	
	private function Say(o){
		if(AutoLayOut.say==undefined){
			trace(o);
		}else{
			AutoLayOut.say(o);
		}
	}

}